package com.tsd.system.service.impl;

import com.tsd.core.constants.Constants;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.HlpException;
import com.tsd.service.BaseServiceImpl;
import com.tsd.system.dao.SqlSelectMapper;
import com.tsd.system.dao.SyncDataMapper;
import com.tsd.system.dao.SysAccountSetMapper;
import com.tsd.system.entity.SysAccountSetExt;
import com.tsd.system.entity.SysUser;
import com.tsd.system.service.SyncDataService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 描述：同步数据服务实现层
 *
 * @author Hillpool
 * @date 2021/01/12 12:04:12
 */
@Service
public class SyncDataServiceImpl extends BaseServiceImpl implements SyncDataService {

    @Resource
    private SqlSelectMapper sqlSelectMapper;
    @Resource
    private SyncDataMapper syncDataMapper;
    @Resource
    private SysAccountSetMapper sysAccountSetMapper;

    public SqlSelectMapper getSqlSelectMapper() {
        return sqlSelectMapper;
    }

    public void setSqlSelectMapper(SqlSelectMapper sqlSelectMapper) {
        this.sqlSelectMapper = sqlSelectMapper;
    }

    static String Blank = " ";

    @Override
    public List<Map<String, Object>> findSyncSrcList(Map<String, Object> params) throws Exception {
        String account_set_sid = HlpUtils.getString(params, Constants.HEADER_NAME_ACCOUNT_SET_SID);
        String table_name = HlpUtils.getString(params, "table_name");
        super.checkEmpty(account_set_sid, "缺少参数account_set_sid");
        super.checkEmpty(table_name, "缺少参数table_name");
        if (table_name.contains(Blank)) {
            throw new HlpException("table_name不符合规范");
        }
        SysAccountSetExt accountSetExt = sysAccountSetMapper.selectBySid(account_set_sid);
        super.checkEmpty(accountSetExt, "账套不存在");
        params.put("db_name", accountSetExt.getDb_name());
        return syncDataMapper.findSyncSrcList(params);
    }

    static String Commas = ",";
    static String ParamTag = "params.";
    static String ParamBegin = "#{";
    static String ParamEnd = "}";

    @Override
    public void saveSyncDataList(String account_set_sid, String table_name, List<Map<String, Object>> list, SysUser opUser) throws Exception {
        super.checkEmpty(account_set_sid, "缺少参数account_set_sid");
        SysAccountSetExt accountSetExt = sysAccountSetMapper.selectBySid(account_set_sid);
        super.checkEmpty(accountSetExt, "账套不存在");
        super.checkEmpty(table_name, "缺少参数table_name");
        if (table_name.contains(Blank)) {
            throw new HlpException("table_name不符合规范");
        }
        for (Map<String, Object> t : list) {
            t.remove("id");
        }
        for (Map<String, Object> t : list) {
            StringBuffer buffer = new StringBuffer();
            StringBuffer buffer2 = new StringBuffer();
            Set<String> keys = t.keySet();
            for (String key : keys) {
                if (key.contains(Blank)) {
                    throw new HlpException("字段名称不符合规范");
                }
                if (buffer.length() > 0) {
                    buffer.append(Commas);
                    buffer2.append(Commas);
                }
                buffer.append(key);
                buffer2.append(ParamBegin);
                buffer2.append(ParamTag + key);
                buffer2.append(ParamEnd);
            }
            String sql = "insert into " + accountSetExt.getDb_name() + "." + table_name + "(" + buffer + ")values(" + buffer2 + ")";
            t.put("sql", sql);
            sqlSelectMapper.insertByParams(t);
        }
    }
}
